package cn.gov.anhai.service;

import cn.gov.anhai.mappers.CleanRhymeMapper;
import cn.gov.anhai.mappers.PublicMapper;
import cn.gov.anhai.utils.MyUtil;
import cn.gov.anhai.utils.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 廉韵安海
 */
@Slf4j
@Service
public class CleanRhymeService {
    @Resource
    PublicMapper publicMapper;
    @Resource
    PublicService publicService;
    @Resource
    CleanRhymeMapper cleanRhymeMapper;

    /**
     * 开屏封面
     */
    public Map<String, Object> back() {
        Map<String, Object> info = cleanRhymeMapper.back();
        if (info == null) return null;
        info.put("image", publicService.getOSSUrl((Long) info.get("image")));
        info.put("duration", (Integer) info.get("duration") * 1000);
        return info;
    }

    /**
     * 默认路线
     */
    public Map<String, Object> defaultInfo() {
        Map<String, Object> data = cleanRhymeMapper.defaultInfo();
        if (data == null) return null;

        Map<String, Object> result = new HashMap<>();
        result.put("map", publicService.getOSSUrl((Long) data.get("map")));
        result.put("foot", publicService.getOSSUrl((Long) data.get("foot")));
        result.put("size", new HashMap<String, Object>() {
            {
                put("width", data.get("size_width"));
                put("height", data.get("size_height"));
            }
        });
        return result;
    }

    /**
     * 所有路线
     */
    public List<Map<String, Object>> list() {
        List<Map<String, Object>> list = cleanRhymeMapper.list();
        list.forEach(item -> {
            item.put("map", publicService.getOSSUrl((Long) item.remove("map")));
            item.put("foot", publicService.getOSSUrl((Long) item.remove("foot")));
            item.put("size", new HashMap<String, Object>() {
                {
                    put("width", item.remove("size_width"));
                    put("height", item.remove("size_height"));
                }
            });
            item.put("scenicList", cleanRhymeMapper.scenicList((Long) item.get("id")));
            item.put("icon", publicService.getOSSUrl((Long) item.remove("icon")));
            item.put("selectedIcon", publicService.getOSSUrl((Long) item.remove("selectedIcon")));

            item.put("hbg", publicService.getOSSUrl((Long) item.remove("hbg")));
            item.put("vbg", publicService.getOSSUrl((Long) item.remove("vbg")));
            item.put("hborder", publicService.getOSSUrl((Long) item.remove("hborder")));
            item.put("vborder", publicService.getOSSUrl((Long) item.remove("vborder")));

            Map<String, Object> contentConfig = new HashMap<>();
            contentConfig.put("dragIcon",publicService.getOSSUrl((Long)item.remove("dragIcon")));//拖拽按钮图片
            contentConfig.put("tabSelectBackColor",item.remove("tabSelectBackColor"));//按钮颜色选中
            contentConfig.put("tabUnselectBackColor",item.remove("tabUnselectBackColor"));//按钮颜色未选中
            contentConfig.put("tabSelectShadowColor",item.remove("tabSelectShadowColor"));//按钮阴影选中
            contentConfig.put("tabUnselectShadowColor",item.remove("tabUnselectShadowColor"));//按钮阴影未选中
            contentConfig.put("introBackColor",item.remove("introBackColor"));//整体背景色
            contentConfig.put("introBorderImg",publicService.getOSSUrl((Long)item.remove("introBorderImg")));//边框角图片
            contentConfig.put("introBorderColor",item.remove("introBorderColor"));//边框线颜色
            contentConfig.put("toggleButtonSelectColor",item.remove("toggleButtonSelectColor"));//轮播指示点选中颜色
            contentConfig.put("toggleButtonUnselectColor",item.remove("toggleButtonUnselectColor"));//轮播指示点未选中颜色
            contentConfig.put("tabSelectFontColor",item.remove("tabSelectFontColor"));//按钮选中字体颜色
            contentConfig.put("tabUnselectFontColor",item.remove("tabUnselectFontColor"));//按钮未选中字体颜色
            contentConfig.put("hborderSelect",publicService.getOSSUrl((Long)item.remove("hborderSelect")));//路线横屏的框选中图片
            item.put("contentConfig", contentConfig);
        });
        return list;
    }

    /**
     * 名人列表
     *
     * @param way_id 路线ID
     */
    public List<Map<String, Object>> famousPersonList(Integer current, Integer size, Long way_id) {
        PageUtil pageUtil = new PageUtil(current, size);
        List<Map<String, Object>> list = cleanRhymeMapper.famousPersonList(pageUtil.getOffset(), pageUtil.getRow(), way_id);
        list.forEach(item -> {
            item.put("cover", publicService.getOSSUrl((Long) item.get("cover")));
            item.put("tags", MyUtil.getString((String) item.get("tags")).split("\\,"));
        });
        return list;
    }

    /**
     * 名人列表（个数）
     *
     * @param way_id 路线ID
     */
    public Integer famousPersonListCount(Long way_id) {
        log.info("=========>" + way_id);
        Integer count = cleanRhymeMapper.famousPersonListCount(way_id);
        return count;
    }

    /**
     * 名人详情
     */
    public Map<String, Object> famousPersonInfo(Long id) {
        Map<String, Object> info = cleanRhymeMapper.famousPersonInfo(id);
        if (info == null) return null;

        Map<String, Object> result = new HashMap<>();
        result.put("id", info.get("id"));
        result.put("name", info.get("name"));
        result.put("cover", publicService.getOSSUrl((Long) info.get("cover")));
        result.put("desc", info.get("intro"));

        return result;
    }
}
